<?php
  require(dirname(__FILE__).'/global.php');
  require(ROOT.'/admin/isLogin.php');
  
  $submit=$_POST['submit'];
  $page_size = 10;
  if(!isset($submit)){          
      $sql = "select * from talking_filter order by FilterID desc limit 0,".$page_size;
      $filters = $db->query($sql);
      
      $smarty->assign('filters',$filters);
      $smarty->assign('current',1);
      $smarty->display('word_filter.html');
  }
  else{
      if($submit == 'page'){
          $current = $_POST['current'];
          $current = strlen($current)==0 ? 1 : $current;
          $current = intval($current);
          $tip = null;              
          if($current<1){
              $tip = '已经是第一页了!';
              $current = 1;
          }
          else{
              $sql = "select count(*) from talking_filter";
              $array2 = $db->query($sql);
                  
              $count = $array2[0][0];
              $count = $count == null ? 0 : $count;
              $pages = ($count+$page_size-1)/$page_size;
              $pages = intval($pages);
              if($current>$pages){
                  $tip = '已经是最后一页了!';
                  $current = $pages;
              }
          }
          $sql = "select * from talking_filter order by FilterID desc limit ".($current-1)*$page_size.",".$page_size;
          $filters = $db->query($sql);
          $message = '';
          foreach($filters as $filter){
              $message .= '<tr>';
              $message .= "<td align='center'>";
              $message .= "<input type='checkbox' name='del[ ]' value='".$filter['FilterID']."' />";
              $message .= "</td>";
              $message .= "<td>".$filter['FilterWords']."</td>";
              $message .= "<td>".$filter['FilterInsteadWords']."</td>";
              $message .= "<td align='center'>".$filter['FilterManager']."</td>";
              $message .= '</tr>'; 
          }                                             
          echo json_encode($message.'@'.$tip.'@'.$current);
      }
      if($submit == 'import'){
          $filename = $_POST['name'];
          $filename = iconv('utf-8','gbk',$filename);
          if(!empty($filename)){
              $file = dirname(__FILE__).'/../attachment/'.$filename;
              if(file_exists($file)){
                  $lines = file($file);
                  $content = '';
                  foreach($lines as $line){
                      $line = rtrim($line);
                      $line = iconv('gbk','utf-8',$line);
                      if(strpos($line,'=')>0){
                          $array=split('=',$line);
                          $sql = $db->getSql('insert_keywords');
                          $sql = $db->setParam('filterWords',$array[0],$sql);
                          $sql = str_replace('insteadWords',$array[1],$sql);
                          $db->execute($sql);
                      }
                  }
                  @unlink($file);
                  echo json_encode('success');
              }
              else{
                  echo json_encode('传入文件名有误!');
              }
          }
      }
      if($submit == 'add'){
          $dels = $_POST['del'];
          $words = $_POST['word'];
          $insteadWords = $_POST['insteadWord'];                                     
          foreach($dels as $del){
              $sql = "delete from talking_filter where FilterID = ".$del;
              $db->execute($sql);
          }
          for($i=0;$i<count($words);$i++){
              $sql = "insert into talking_filter(FilterWords,FilterInsteadWords,FilterManager) values('".$words[$i]."','".$insteadWords[$i]."','".$_SESSION['admin']."')";
              $db->execute($sql);
          }
          
          $sql = "select * from talking_filter order by FilterID desc limit 0,".$page_size;
          $filters = $db->query($sql);
          $message = '';
          foreach($filters as $filter){
              $message .= '<tr>';
              $message .= "<td align='center'>";
              $message .= "<input type='checkbox' name='del[ ]' value='".$filter['FilterID']."' />";
              $message .= "</td>";
              $message .= "<td>".$filter['FilterWords']."</td>";
              $message .= "<td>".$filter['FilterInsteadWords']."</td>";
              $message .= "<td align='center'>".$filter['FilterManager']."</td>";
              $message .= '</tr>'; 
          }                                             
          echo json_encode($message);
      }
  }
?>